// node.js创建服务
const http = require("http")
var fs = require("fs")
const { Socket } = require("dgram")
const app = http.createServer()

// 监听request事件
app.on("request", (req, res) => {
    fs.readFile(__dirname + "/index.html", function(err, data){
        if(err) {
            res.writeHead(500)
            return res.end("Error loading index.html")
        }
        res.writeHead(200)
        res.end(data)
    })
})


var io = require("socket.io")(app)
io.on("connection", socket => {
    console.log("有用户连接了")
    socket.emit("send", {name: "zs"})
    socket.on("hehe", data => {
        console.log("收到前台传来数据", data)
    })
})

app.listen(3000, () => {
    console.log("服务启动成功")
})

